/**
 * 
 */
package jpb;

import static jpb.Assert.notNull;

/**
 * Exception raised by {@link jpb.JvmArg} for invalid JVM argument values. 
 * 
 * @author Dariusz Kordonski
 */
public class InvalidJvmArgException extends IllegalArgumentException {

    private static final long serialVersionUID = -5883188876024926279L;

    private static final String MSG_TEMPLATE = "JVM arg value <%s> of type %s is invalid: %s";

    private final JvmArg<?> jvmArg;
    private final Object value;
    private final String reason;



    public InvalidJvmArgException(JvmArg<?> jvmArg, Object value, String reason) {
        super(String.format(MSG_TEMPLATE, value, jvmArg, reason));
        this.jvmArg = notNull("jvmArg", jvmArg);
        this.value = notNull("value", value);
        this.reason = notNull("reason", reason);
    }

    public JvmArg<?> jvmArg() {
        return jvmArg;
    }

    public Object value() {
        return value;
    }

    public String reason() {
        return reason;
    }

    public <T> T value(JvmArg<T> arg) {
        if (!this.jvmArg.equals(arg)) {
            throw new IllegalArgumentException(arg + " does not match the underlying jvmArg: " + jvmArg);
        }
        return arg.argumentClass().cast(value);
    }

}
